﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticPathPlanner.Planner.CApAPPMagnetPoint
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.AutomaticPathPlanner.Planner;

internal class CApAPPMagnetPoint
{
  private readonly CApAPPObjectFlowTask m_objectFlowTask;
  private static TxPlacementManipulator m_manipulator;
  private readonly ITxObject m_ghost;
  private bool m_isActivated;
  private bool m_isAlived;
  private TxTransformation m_magnetPointPosition;

  public CApAPPMagnetPoint(CApAPPObjectFlowTask objectFlowTask)
  {
    this.m_objectFlowTask = objectFlowTask;
    CApAPPMagnetPoint.m_manipulator = new TxPlacementManipulator();
    this.m_ghost = this.m_objectFlowTask.CreateMagnetPointGhost();
    if (this.m_ghost == null)
      throw new CApAPPMagnetPoint.MagnetPointCreationFailedException();
    new TxObjectTreeInternal().ExcludeNode(this.m_ghost);
    this.m_magnetPointPosition = new TxTransformation();
    this.m_isActivated = false;
    this.m_isAlived = true;
  }

  public void Activate()
  {
    if (!this.m_isAlived)
      return;
    (this.m_ghost as ITxDisplayableObject).Display();
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add(this.m_ghost);
    TxPlacementManipulatorParameters manipulatorParameters = new TxPlacementManipulatorParameters((TxObjectList<ITxObject>) txObjectList);
    CApAPPMagnetPoint.m_manipulator.ManipulatorMoved += new TxPlacementManipulator_ManipulatorMovedEventHandler(this.Placement_ManipulatorMoved);
    CApAPPMagnetPoint.m_manipulator.Show(manipulatorParameters);
    CApAPPMagnetPoint.m_manipulator.RotationStepSize = 5.0;
    CApAPPMagnetPoint.m_manipulator.TranslationStepSize = 5.0;
    CApAPPMagnetPoint.m_manipulator.SnapByStepSize = false;
    this.m_isActivated = true;
  }

  public void Deactivate()
  {
    if (!this.m_isAlived)
      return;
    CApAPPMagnetPoint.m_manipulator.Hide();
    (this.m_ghost as ITxDisplayableObject).Blank();
    this.m_objectFlowTask.DisableMagnetPoint();
    CApAPPMagnetPoint.m_manipulator.ManipulatorMoved -= new TxPlacementManipulator_ManipulatorMovedEventHandler(this.Placement_ManipulatorMoved);
    this.m_isActivated = false;
  }

  public bool IsActivated() => this.m_isActivated;

  public void SetPosition(TxTransformation newPosition)
  {
    if (!this.m_isAlived)
      return;
    this.m_objectFlowTask.MagnetPointPosition = newPosition;
    this.m_magnetPointPosition = newPosition;
    this.UpdateGhostPosition(newPosition);
  }

  public void ClearData()
  {
    if (!this.m_isAlived)
      return;
    this.Deactivate();
    this.m_ghost.Delete();
    this.m_isAlived = false;
  }

  private void Placement_ManipulatorMoved(
    object sender,
    TxPlacementManipulator_ManipulatorMovedEventArgs args)
  {
    this.m_objectFlowTask.MagnetPointPosition = TxTransformation.op_Multiply(this.m_magnetPointPosition, args.DeltaTransformation);
    this.m_magnetPointPosition = TxTransformation.op_Multiply(this.m_magnetPointPosition, args.DeltaTransformation);
    this.UpdateGhostPosition(this.m_magnetPointPosition);
  }

  private void UpdateGhostPosition(TxTransformation magnetPointPosition)
  {
    (this.m_ghost as ITxLocatableObject).AbsoluteLocation = TxTransformation.op_Multiply(magnetPointPosition, this.m_objectFlowTask.RelativeGripPosition.Inverse);
  }

  public class MagnetPointCreationFailedException : Exception
  {
  }
}
